package com.ocom.accesservice.controller;


import com.ocom.accesservice.service.AreaInfoService;
import com.ocom.accesservice.service.MjDeviceInfoService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.response.wx.MachInfoPageOnShowResponse;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.AreaInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MachineController {

    @Autowired
    MjDeviceInfoService mjDeviceInfoService;



    @Autowired
    AreaInfoService areaInfoService;


    /**
     * 企业所有机器数量
     * @param companyId
     * @return
     */
    @PostMapping(AccessServiceUrl.URL_getMachineCountById)
    public Result getCountByComId(Long companyId){
        return ResultUtil.success(mjDeviceInfoService.getCountByComId(companyId));
    }


    @PostMapping(AccessServiceUrl.URL_getMachineListOnLoad)
    public Result getMachineList(Long companyId){
        return ResultUtil.success(mjDeviceInfoService.getMachineListOnLoad(companyId));
    }

    @PostMapping(AccessServiceUrl.URL_getMachineInfoByMachNo)
    public Result<MachInfoPageOnShowResponse> getMachineInfoById(Long companyId,String machNo){

        EntityMjDeviceInfo entityMjDeviceInfo=mjDeviceInfoService.getMachineInfoByMachNo(companyId,machNo);
        if(entityMjDeviceInfo==null){
            return ResultUtil.error(ResultEnum.Err_2013);
        }

        MachInfoPageOnShowResponse machInfoPageOnShowResponse=new MachInfoPageOnShowResponse();
        machInfoPageOnShowResponse.setMachNo(entityMjDeviceInfo.getDeviceNo());

        machInfoPageOnShowResponse.setOpenType(entityMjDeviceInfo.getOpenType());

        if(entityMjDeviceInfo.getStatus()>0){
            machInfoPageOnShowResponse.setMachType(0);
        }else{
            if(entityMjDeviceInfo.getAreaId()==0L){
                machInfoPageOnShowResponse.setMachType(1);
            }else{
                machInfoPageOnShowResponse.setMachType(2);
            }
        }
        String date="";
        if(entityMjDeviceInfo.getEndTime()==null){
            machInfoPageOnShowResponse.setDateColor("");
        }else{
            if(new Date().compareTo(entityMjDeviceInfo.getEndTime())<=0){
                machInfoPageOnShowResponse.setDateColor("red");
            }
            date= DateUtil.dateToStr(entityMjDeviceInfo.getEndTime(),null);
        }
        machInfoPageOnShowResponse.setEndDate(date);
        machInfoPageOnShowResponse.setMachName(entityMjDeviceInfo.getDeviceName());

        machInfoPageOnShowResponse.setWifiSetup(true);

        if(entityMjDeviceInfo.getAreaId()>0L){
            EntityMjAreaInfo entityMjAreaInfo=areaInfoService.getAreaInfoById(companyId,entityMjDeviceInfo.getAreaId());
            if(entityMjAreaInfo!=null){
                machInfoPageOnShowResponse.setPlace(entityMjAreaInfo.getAreaName());
            }
        }

        if(entityMjDeviceInfo.getStatus()>0L){
            machInfoPageOnShowResponse.setQrVerify(true);
        }else{
            machInfoPageOnShowResponse.setQrVerify(false);
        }

        List<AreaInfoVo> areaInfoVoList = areaInfoService.getAreaList(companyId);

        if(entityMjDeviceInfo.getAreaId()>0L) {
            AreaInfoVo areaInfoVo = new AreaInfoVo();
            areaInfoVo.setAreaName("转备用机");
            areaInfoVo.setAreaNo(0L);
            areaInfoVoList.add(areaInfoVo);
        }
        machInfoPageOnShowResponse.setPlaceList(areaInfoVoList);

        return ResultUtil.success(machInfoPageOnShowResponse);
    }

    @PostMapping(AccessServiceUrl.URL_updateMachNameByMachNo)
    public  Result updateMachNameByMachNo(Long  companyId,String machNo, String machName){
        EntityMjDeviceInfo entityMjDeviceInfo=mjDeviceInfoService.updateMachNameByMachNo(companyId,machNo,machName);
        if(entityMjDeviceInfo==null){
            return ResultUtil.error(ResultEnum.Err_2013);
        }
        return ResultUtil.success(entityMjDeviceInfo);
    }

    @PostMapping(AccessServiceUrl.URL_WXchangeMachPlace)
    public  Result  changeMachPlace(Long companyId,String machNo,Long areaNo){
        return mjDeviceInfoService.updateMachPlace(companyId,machNo,areaNo);
    }

    @PostMapping(AccessServiceUrl.URL_WXremoveMach)
    public  Result   removeMach(Long companyId,String machNo){
        //判断是否可以移除
        boolean b=mjDeviceInfoService.canRemoveMach(companyId,machNo);
        if(!b){
            return ResultUtil.error(ResultEnum.Err_2017);
        }

        EntityMjDeviceInfo entityMjDeviceInfo = mjDeviceInfoService.removeMachPartDb(companyId, machNo);
        return ResultUtil.success(entityMjDeviceInfo);

    }

    // 更改设备参数状态（需下发）（setMachParaState）
    @PostMapping(value = AccessServiceUrl.URL_SetMachParaState)
    public Result SetMachParaState(Long companyId, String machNo, String jobId, Long state){
        if (companyId==null || machNo==null || jobId == null || state == null){
            return ResultUtil.error(ResultEnum.Err_2013);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            mjDeviceInfoService.SetMachParaState(companyId, machNo,jobId,state);
            map.put("updataFlag",1);
        }catch (Exception exception){
            log.error("更改设备参数状态失败："+exception.getMessage());
            map.put("updataFlag",0);
        }
        return ResultUtil.success(map);
    }

}
